<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>声级计数据管理后台</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        
        .container {
            width: 80%;
            margin: auto;
            background-color: #f5f5f5;
            border: 1px solid #ccc;
            padding: 20px;
        }
        
        h2 {
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .form-group {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        
        .form-group label {
            margin-right: 10px;
        }
        
        select {
            flex-grow: 1;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #eee;
        }
        
        button {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #333;
            color: white;
            cursor: pointer;
        }
    </style>
    <script>
        function displayDate(){
            document.getElementById("time").innerHTML=Date();
        }
        //软件版本
        document.addEventListener('DOMContentLoaded', function() {
            const versionDisplay = document.getElementById('version');

            function fetchSoftwareVersion() {
                fetch('/api/Software_version')
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok');
                        }
                        return response.json();
                    })
                    .then(data => {
                        versionDisplay.textContent = `${data.software_version}`;
                    })
                    .catch(error => {
                        console.error('There has been a problem with your fetch operation:', error);
                        versionDisplay.textContent = '无法获取版本信息';
                    });
            }

            // 获取软件版本信息
            fetchSoftwareVersion();
        });

        //手动获取数据
        document.addEventListener('DOMContentLoaded', function() {
            const getDataButton = document.getElementById('getDataButton');
            const soundLevelDisplay = document.getElementById('soundLevelDisplay');

            getDataButton.addEventListener('click', function() {
                fetch('/api/sound-level')
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok');
                        }
                        return response.json();
                    })
                    .then(data => {
                        soundLevelDisplay.textContent = `${data.sound_level} dB`;
                    })
                    .catch(error => {
                        console.error('There has been a problem with your fetch operation:', error);
                    });
            });
        });
        //自动获取数据
        document.addEventListener('DOMContentLoaded', function() {
            const scalesCheckbox = document.getElementById('scales');
            const tentaclesInput = document.getElementById('tentacles');
            const soundLevelDisplay = document.getElementById('soundLevelDisplay_auto');
            let intervalId = null;

            function fetchSoundLevel() {
                fetch('/api/sound-level')
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok');
                        }
                        return response.json();
                    })
                    .then(data => {
                        soundLevelDisplay.textContent = `${data.sound_level} dB`;
                    })
                    .catch(error => {
                        console.error('There has been a problem with your fetch operation:', error);
                    });
            }

            function updateInterval() {
                const interval = parseInt(tentaclesInput.value, 10);
                if (scalesCheckbox.checked && !isNaN(interval)) {
                    clearInterval(intervalId);
                    intervalId = setInterval(fetchSoundLevel, interval);
                } else {
                    // 如果复选框未选中，清除定时器
                    clearInterval(intervalId);
                }
            }

            scalesCheckbox.addEventListener('change', updateInterval);
            tentaclesInput.addEventListener('input', updateInterval);

            // 初始化时检查复选框状态
            updateInterval();
        });
    </script>
</head>
<body>
    <div class="container">
        
        <section class="content">
            <h1>声级计数据管理后台</h1>
            <HR>
                <h2>串口设置</h2>
                <form action="#">
                    <div class="form-group">
                        <label for="port">串口号：</label>
                        <select id="port" name="port">
                            <option value="/dev/ttyS0">/dev/ttyS0</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="baudrate">波特率：</label>
                        <select id="baudrate" name="baudrate">
                            <option value="9600">9600</option>
                            <option value="19200">19200</option>
                            <option value="38400">38400</option>
                            <option value="57600">57600</option>
                            <option value="115200">115200</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="data_bits">数据位：</label>
                        <select id="data_bits" name="data_bits">
                            <option value="8">8位</option>
                            <option value="7">7位</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="parity">校验位：</label>
                        <select id="parity" name="parity">
                            <option value="None">无</option>
                            <option value="Even">偶校验</option>
                            <option value="Odd">奇校验</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="stop_bits">停止位：</label>
                        <select id="stop_bits" name="stop_bits">
                            <option value="1">1位</option>
                            <option value="2">2位</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="flow_control">流控位：</label>
                        <select id="flow_control" name="flow_control">
                            <option value="None">无</option>
                            <option value="RTS/CTS">RTS/CTS</option>
                            <option value="XON/XOFF">XON/XOFF</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <button type="submit" onclick="get_Serial_port_info()">保存设置</button>
                        <button type="submit" >刷新配置</button>
                        <button type="submit" onclick="displayDate()">显示日期</button>
                    </div>
                    <div class="form-group">
                        <p id="time"> </p>
                    </div>
                    
                </form>
            <HR>
            <div>
                <h2>实时数据</h2>

                <div>
                    <div class="form-group">
                        <input type="checkbox" id="scales" name="scales"/>
                        <label for="scales">自动获取声级计</label>
                        <label>采集间隔时间（ms）：</label>
                        <input type="number" id="tentacles" name="tentacles" value="1000"/>
                    </div>

                    <div class="form-group">
                        <p>当前声压值：</p>
                        <p id="soundLevelDisplay_auto"></p>
                    </div>
                </div>
                <HR>

                <button id="getDataButton">获取实时声压级数据</button>
                <div class="form-group">
                    <p>当前声压值：</p>
                    <p id="soundLevelDisplay"></p>
                </div>
                <HR>
                <div class="form-group">
                    <label>后端软件版本：</label>
                    <p id="version">正在获取版本信息...</p>
                </div>
            </div>

        </section>
    </div>
</body>
</html>

